source("020-prepare_data.R")

# SP global by year -------------------------------------------------------
# Only count the SP signed, regardless the actors
sp_times_year <- sp_count_data %>%
  group_by(year) %>%
  summarise(times = n()) %>%
  ungroup() %>%
  mutate(cumulative_sum = cumsum(times))

ggplot(sp_times_year, aes(x = year)) +
  geom_bar(stat = "identity", aes(y = times)) +
  geom_line(aes(y = cumulative_sum / 5)) +
  scale_fill_manual(values = c("#3C3C3C")) +
  scale_linetype_manual(values = c(1)) +
  scale_y_continuous(name = "Annual number",
                     sec.axis = sec_axis(~ . * 5, name = "Cumulative number")) +
  scale_x_continuous(breaks = seq(1990, 2020, 5)) +
  labs(fill = "Annual number", linetype = "Cumulative number") +
  guides(
    fill = guide_legend(title.position = "left", order = 1),
    linetype = guide_legend(title.position = "left", order = 2)
  )
save_plot("sp_aggregate.png")

# Top 10 country ----------------------------------------------------------

top_10 <- country_year_signed_sp_sum %>%
  filter(year == 2020) %>%
  select(country, country_cum) %>%
  mutate(country_name = factor(
    country,
    levels = country,
    labels = countrycode::countrycode(
      country,
      origin = "iso3c",
      destination = "country.name.en",
      custom_match = c("GE-AB" = "Abkhazia", "KOSOVO" = "Kosovo")
    )
  )) %>%
  top_n(n = 10, wt = country_cum) %>%
  arrange(desc(country_cum)) %>%
  filter(country != "KAZ")

ggplot(top_10, aes(x = reorder(country_name, desc(country_cum)), y = country_cum)) +
  geom_bar(stat = "identity") +
  geom_text(aes(label = country_cum), nudge_y = 10) +
  labs(x = "Top 10 Country", y = "# of Partnership Agreements")
save_plot("top_10.png")

# draw map ----------------------------------------------------------------
# Kinne(2018, 807)
year_seq <- seq(2000, 2020, 10)
sp_for_map_data <- country_year_signed_sp_sum %>%
  filter(year %in% year_seq) %>%
  mutate(country = if_else(country == "Kosovo", "KOS", country))

world_map <- rnaturalearth::ne_countries(returnclass = "sf") %>%
  mutate(country = adm0_a3) %>%
  filter(!country %in% c("ATA", "GRL")) %>%
  select(country, geometry) %>%
  uncount(length(year_seq)) %>%
  group_by(country) %>%
  mutate(year = row_number(),
         year = year_seq[year]) %>%
  left_join(sp_for_map_data, by = c("country", "year")) %>%
  mutate(
    country_cum = replace_na(country_cum, 0),
    ln_country_cum = log(country_cum),
    ln_country_cum = if_else(ln_country_cum == -Inf, 0, ln_country_cum)
  )
half_line = 11 / 4

# Grey
ggplot(world_map) +
  facet_wrap(~year, dir = "v") +
  geom_sf(aes(geometry = geometry, fill = ln_country_cum)) +
  geom_text(aes(label = year, x = -130, y = -10), size = 10) +
  scale_fill_gradient(low = "#D2D2D2",
                      high = "#3C3C3C",
                      name = "Log Signed Agreements") +
  theme(
    strip.text.x = element_blank(),
    axis.text.x = element_blank(),
    axis.title.y.left = element_blank(),
    axis.ticks = element_blank(),
    axis.line.x = element_blank(),
    panel.grid = element_blank(),
    axis.text = element_blank(),
    plot.background = element_rect(fill = "white", color = NA)
  )

save_plot("map_changing_log_grey.png",
          height = 16,
          width = 12)


